﻿using System;
using System.Drawing.Imaging;
using CxRomos.Core.Services;
using StructureMap;

namespace CxRomos.Web.Account.UserControls {
    public partial class JpegImage : System.Web.UI.Page {
        readonly Random _random = new Random();
        private IWebContext _webContext;
        protected void Page_Load(object sender, EventArgs e) {
            _webContext = ObjectFactory.GetInstance<IWebContext>();
            _webContext.CaptchaImageText = GenerateRandomCode();

            var ci = ObjectFactory.GetInstance<ICaptcha>();
            ci.Text = _webContext.CaptchaImageText;
            ci.Width = 200;
            ci.Height = 50;
            ci.FamilyName = "Verdana";

            Response.Clear();
            Response.ContentType = "image/jpeg";
            ci.Image.Save(Response.OutputStream, ImageFormat.Jpeg);
            ci.Dispose();
        }

        private string GenerateRandomCode() {
            var s = "";
            for(var i = 0; i < 6; i++) {
                s = String.Concat(s, _random.Next(10).ToString());
            }
            return s;
        }

        override protected void OnInit(EventArgs e) {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent() {
            Load += Page_Load;
        }

    }
}
